package com.meirengu.webview.service.impl;

import com.meirengu.channel.service.AnyidaiService;
import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.KwAydFilter;
import com.meirengu.webview.model.KwInterfaceConfig;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwAydFilterService;
import com.meirengu.webview.service.KwAydPushService;
import com.meirengu.webview.service.UserInfoService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * KwAydFilter服务实现层
 *
 * @author Mao
 * @create Wed May 09 11:16:35 CST 2018
 */
@Service
public class KwAydFilterServiceImpl extends BaseServiceImpl<KwAydFilter> implements KwAydFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private AnyidaiService anyidaiService;

    @Autowired
    private KwAydPushService kwAydPushService;

    public static final String HUIJIE_CHANNEL_CODE = "huijie";

    public static final String HUIJIE_CHANNEL_NAME = "惠借";

    private String[] cycleTimeValue = new String[]{"90", "180", "270", "360", "720"};

    Logger logger = LoggerFactory.getLogger(KwAydFilterServiceImpl.class);


    /**
     * 发送安易贷接口
     * 过滤条件
     * 1大于5w
     * 2城市
     * 3公积金或保单2其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendAnyidai(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String aydIol = redisClient.get("iol-ayd");
            if ("N".equals(aydIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthday = sdf.format(brithdayDate);
                String city = userInfo.getScity();
                String ip = userInfo.getIp();
                Date registerTime = user.getCreateDate();
                String currentTime = String.valueOf(registerTime.getTime());
                currentTime = currentTime.substring(0, 10);
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Integer occupation = userInfo.getOccupation();
                Integer policy = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer salaryMethod = userInfo.getSalaryMethod();
                Integer creditCard = userInfo.getCredit();
                Integer salary = userInfo.getSalary();
                Integer car = userInfo.getCar();
                Integer sex = user.getSex();
                Integer workDuration = userInfo.getWorkDuration();

                KwAydFilter kwAydFilter = new KwAydFilter();
                kwAydFilter.setUserId(id);
                kwAydFilter.setMobile(mobile);
                kwAydFilter.setChannelCode("ayd");
                kwAydFilter.setChannelName("安易贷");

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("ayd");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwAydFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwAydFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwAydFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + ":" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        //名下房产  1 无房   2 有房贷   3有房无贷
                        //住房公积金   0无公积金   1一年以内    2一年以上
                        //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getFundValue().contains(fund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {

                            String strSex = "男";
                            if (sex.intValue() == 1) {
                                strSex = "女";
                            }
                            String hasHouse = "无房";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 2) {
                                hasHouse = "有房";
                            } else if (house.intValue() == 3) {
                                hasHouse = "有房无贷";
                            }
                            String isWxloan = "无";
                            if (wxloan.intValue() == 1) {
                                isWxloan = "有";
                            }
                            String isOccuption = "白领";
                            if (occupation.intValue() == 1) {
                                isOccuption = "私企业主";
                            } else if (occupation.intValue() == 2) {
                                isOccuption = "公务员";
                            }
                            String isPolicy = "无";
                            if (policy.intValue() == 2) {
                                isPolicy = "年缴2400以下";
                            } else if (policy == 3) {
                                isPolicy = "年缴2400以上";
                            }
                            String isFund = "无公积金";
                            if (fund.intValue() == 1) {
                                isFund = "一年以下";
                            } else if (fund.intValue() == 2) {
                                isFund = "一年以上";
                            }
                            String isSocialSecurity = "无社保";
                            if (socialSecurity.intValue() == 1) {
                                isSocialSecurity = "一年以下";
                            } else if (socialSecurity.intValue() == 2) {
                                isSocialSecurity = "一年以上";
                            }
                            String isSalaryMethod = "打卡";
                            if (salaryMethod.intValue() == 2) {
                                isSalaryMethod = "现金";
                            }
                            String isCredit = "无信用卡无贷款";
                            if (creditCard.intValue() == 2) {
                                isCredit = "有信用卡或有贷款";
                            }
                            String userSalary = "3000以下";
                            if (salary.intValue() == 2) {
                                userSalary = "3000-5000";
                            } else if (salary.intValue() == 3) {
                                userSalary = "5001-8000";
                            } else if (salary.intValue() == 4) {
                                userSalary = "8001-10000";
                            } else if (salary.intValue() == 5) {
                                userSalary = "1万以上";
                            }
                            String isCar = "无车";
                            if (car.intValue() == 2) {
                                isCar = "有车贷";
                            } else if (car.intValue() == 3) {
                                isCar = "有车无贷";
                            }
                            String workTime = "一年以上";
                            if (workDuration.intValue() == 24) {
                                workTime = "两年以上";
                            }
                            Integer code = anyidaiService.applyLoan(mobile, expectAmount, name, strSex, birthday, city, hasHouse, isWxloan, isOccuption,
                                    workTime, isPolicy, isFund, isSocialSecurity, isSalaryMethod, isCredit, userSalary, isCar, ip);
                            kwAydFilter.setSuited("Y");
                            kwAydFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "ayd", user);
                            if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("用户公积金：" + fund.intValue() + "  用户寿险：" + lifeInsurance.intValue() + "用户房：" + house.intValue());
                        }
                    }
                }
                insert(kwAydFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    /**
     * 发送安易贷2接口
     * 过滤条件
     * 1大于3w 小于5w
     * 2城市
     * 3公积金或保单2其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendAnyidai2(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            //校验是否有ayd发送成功的记录
            Map<String, Object> aydparams = new HashMap<>();
            aydparams.put("mobile", user.getMobile());
            aydparams.put("code", 1);
            Integer count = kwAydPushService.getCount(aydparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count)) {
                return sendFlag;
            }
            String ayd2Iol = redisClient.get("iol-ayd2");
            if ("N".equals(ayd2Iol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthday = sdf.format(brithdayDate);
                String city = userInfo.getScity();
                String ip = userInfo.getIp();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Integer occupation = userInfo.getOccupation();
                Integer policy = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer salaryMethod = userInfo.getSalaryMethod();
                Integer creditCard = userInfo.getCredit();
                Integer salary = userInfo.getSalary();
                Integer car = userInfo.getCar();
                Integer sex = user.getSex();
                Integer workDuration = userInfo.getWorkDuration();

                KwAydFilter kwAydFilter = new KwAydFilter();
                kwAydFilter.setUserId(id);
                kwAydFilter.setMobile(mobile);
                kwAydFilter.setChannelCode("ayd2");
                kwAydFilter.setChannelName("安易贷2");

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("ayd2");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwAydFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwAydFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() >= kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwAydFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + ":" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        //名下房产  1 无房   2 有房贷   3有房无贷
                        //住房公积金   0无公积金   1一年以内    2一年以上
                        //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getFundValue().contains(fund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getSocialSecurityValue().contains(socialSecurity.intValue() + "")) {

                            String strSex = "男";
                            if (sex.intValue() == 1) {
                                strSex = "女";
                            }
                            String hasHouse = "无房";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 2) {
                                hasHouse = "有房";
                            } else if (house.intValue() == 3) {
                                hasHouse = "有房无贷";
                            }
                            String isWxloan = "无";
                            if (wxloan.intValue() == 1) {
                                isWxloan = "有";
                            }
                            String isOccuption = "白领";
                            if (occupation.intValue() == 1) {
                                isOccuption = "私企业主";
                            } else if (occupation.intValue() == 2) {
                                isOccuption = "公务员";
                            }
                            String isPolicy = "无";
                            if (policy.intValue() == 2) {
                                isPolicy = "年缴2400以下";
                            } else if (policy == 3) {
                                isPolicy = "年缴2400以上";
                            }
                            String isFund = "无公积金";
                            if (fund.intValue() == 1) {
                                isFund = "一年以下";
                            } else if (fund.intValue() == 2) {
                                isFund = "一年以上";
                            }
                            String isSocialSecurity = "无社保";
                            if (socialSecurity.intValue() == 1) {
                                isSocialSecurity = "一年以下";
                            } else if (socialSecurity.intValue() == 2) {
                                isSocialSecurity = "一年以上";
                            }
                            String isSalaryMethod = "打卡";
                            if (salaryMethod.intValue() == 2) {
                                isSalaryMethod = "现金";
                            }
                            String isCredit = "无信用卡无贷款";
                            if (creditCard.intValue() == 2) {
                                isCredit = "有信用卡或有贷款";
                            }
                            String userSalary = "3000以下";
                            if (salary.intValue() == 2) {
                                userSalary = "3000-5000";
                            } else if (salary.intValue() == 3) {
                                userSalary = "5001-8000";
                            } else if (salary.intValue() == 4) {
                                userSalary = "8001-10000";
                            } else if (salary.intValue() == 5) {
                                userSalary = "1万以上";
                            }
                            String isCar = "无车";
                            if (car.intValue() == 2) {
                                isCar = "有车贷";
                            } else if (car.intValue() == 3) {
                                isCar = "有车无贷";
                            }
                            String workTime = "一年以上";
                            if (workDuration.intValue() == 24) {
                                workTime = "两年以上";
                            }
                            Integer code = anyidaiService.applyLoan2(mobile, expectAmount, name, strSex, birthday, city, hasHouse, isWxloan, isOccuption,
                                    workTime, isPolicy, isFund, isSocialSecurity, isSalaryMethod, isCredit, userSalary, isCar, ip);
                            kwAydFilter.setSuited("Y");
                            kwAydFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "ayd2", user);
                            if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("用户公积金：" + fund.intValue() + "  用户寿险：" + lifeInsurance.intValue() + "用户房：" + house.intValue());
                        }
                    }
                }
                insert(kwAydFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    /**
     * 发送安易贷3接口
     * 过滤条件
     * 1大于3w
     * 2城市
     * 3有车
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendAnyidai3(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            //校验是否有ayd发送成功的记录
            Map<String, Object> aydparams = new HashMap<>();
            aydparams.put("mobile", user.getMobile());
            aydparams.put("code", 1);
            Integer count = kwAydPushService.getCount(aydparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count)) {
                return sendFlag;
            }
            String ayd3Iol = redisClient.get("iol-ayd3");
            if ("N".equals(ayd3Iol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthday = sdf.format(brithdayDate);
                String city = userInfo.getScity();
                String ip = userInfo.getIp();
                Date registerTime = user.getCreateDate();
                String currentTime = String.valueOf(registerTime.getTime());
                currentTime = currentTime.substring(0, 10);
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Integer occupation = userInfo.getOccupation();
                Integer policy = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer salaryMethod = userInfo.getSalaryMethod();
                Integer creditCard = userInfo.getCredit();
                Integer salary = userInfo.getSalary();
                Integer car = userInfo.getCar();
                Integer sex = user.getSex();
                Integer workDuration = userInfo.getWorkDuration();

                KwAydFilter kwAydFilter = new KwAydFilter();
                kwAydFilter.setUserId(id);
                kwAydFilter.setMobile(mobile);
                kwAydFilter.setChannelCode("ayd3");
                kwAydFilter.setChannelName("安易贷3");

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("ayd3");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwAydFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwAydFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() >= kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwAydFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + ":" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        //名下房产  1 无房   2 有房贷   3有房无贷
                        //住房公积金   0无公积金   1一年以内    2一年以上
                        //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getFundValue().contains(fund.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "") ||
                                kwInterfaceConfig.getSocialSecurityValue().contains(socialSecurity.intValue() + "") ||
                                kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {

                            String strSex = "男";
                            if (sex.intValue() == 1) {
                                strSex = "女";
                            }
                            String hasHouse = "无房";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 2) {
                                hasHouse = "有房";
                            } else if (house.intValue() == 3) {
                                hasHouse = "有房无贷";
                            }

                            String isWxloan = "无";
                            if (wxloan.intValue() == 1) {
                                isWxloan = "有";
                            }

                            String isOccuption = "白领";
                            if (occupation.intValue() == 1) {
                                isOccuption = "私企业主";
                            } else if (occupation.intValue() == 2) {
                                isOccuption = "公务员";
                            }

                            String isPolicy = "无";
                            if (policy.intValue() == 2) {
                                isPolicy = "年缴2400以下";
                            } else if (policy == 3) {
                                isPolicy = "年缴2400以上";
                            }

                            String isFund = "无公积金";
                            if (fund.intValue() == 1) {
                                isFund = "一年以下";
                            } else if (fund.intValue() == 2) {
                                isFund = "一年以上";
                            }

                            String isSocialSecurity = "无社保";
                            if (socialSecurity.intValue() == 1) {
                                isSocialSecurity = "一年以下";
                            } else if (socialSecurity.intValue() == 2) {
                                isSocialSecurity = "一年以上";
                            }

                            String isSalaryMethod = "打卡";
                            if (salaryMethod.intValue() == 2) {
                                isSalaryMethod = "现金";
                            }

                            String isCredit = "无信用卡无贷款";
                            if (creditCard.intValue() == 2) {
                                isCredit = "有信用卡或有贷款";
                            }

                            String userSalary = "3000以下";
                            if (salary.intValue() == 2) {
                                userSalary = "3000-5000";
                            } else if (salary.intValue() == 3) {
                                userSalary = "5001-8000";
                            } else if (salary.intValue() == 4) {
                                userSalary = "8001-10000";
                            } else if (salary.intValue() == 5) {
                                userSalary = "1万以上";
                            }

                            String isCar = "无车";
                            if (car.intValue() == 2) {
                                isCar = "有车贷";
                            } else if (car.intValue() == 3) {
                                isCar = "有车无贷";
                            }

                            String workTime = "一年以上";
                            if (workDuration.intValue() == 24) {
                                workTime = "两年以上";
                            }

                            Integer code = anyidaiService.applyLoan3(mobile, expectAmount, name, strSex, birthday, city, hasHouse, isWxloan, isOccuption,
                                    workTime, isPolicy, isFund, isSocialSecurity, isSalaryMethod, isCredit, userSalary, isCar, ip);
                            kwAydFilter.setSuited("Y");
                            kwAydFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "ayd3", user);
                            if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("用户公积金：" + fund.intValue() + "  用户寿险：" + lifeInsurance.intValue() + "用户房：" + house.intValue() + "用户车：" + car.intValue());
                        }
                    }
                }
                insert(kwAydFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }




    /**
     * 发送1闪借
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendShanjie(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String shanjieIol = redisClient.get("iol-shanjie");
            if ("N".equals(shanjieIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                String city = userInfo.getAreaName();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Integer occupation = userInfo.getOccupation();
                Integer policy = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer salaryMethod = userInfo.getSalaryMethod();
                Integer creditCard = userInfo.getCredit();
                Integer salary = userInfo.getSalary();
                Integer car = userInfo.getCar();
                Integer sex = user.getSex();
                Integer workDuration = userInfo.getWorkDuration();

                KwAydFilter kwAydFilter = new KwAydFilter();
                kwAydFilter.setUserId(id);
                kwAydFilter.setMobile(mobile);
                kwAydFilter.setChannelCode("shanjie");
                kwAydFilter.setChannelName("闪借");

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("shanjie");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwAydFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwAydFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwAydFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwAydFilter.setSuited("N");
                            kwAydFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwAydFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }
                    if (dataFlag) {
                        String isCar = "有";
                        if (car.intValue() == 1) {
                            isCar = "无";
                        }
                        String job = "有";
                        String hasHouse = "有";
                        //名下房产  1 无房   2 有房贷   3有房无贷
                        if (house.intValue() == 1) {
                            hasHouse = "无";
                        }
                        String isPolicy = "有";
                        if (policy.intValue() == 1) {
                            isPolicy = "无";
                        }
                        String strSex = "男";
                        if (sex.intValue() == 1) {
                            strSex = "女";
                        }
                        int loanAmount = (expectAmount.divide(new BigDecimal(10000))).intValue();
                        String isSocialSecurity = "有";
                        if (socialSecurity.intValue() == 0) {
                            isSocialSecurity = "无";
                        }
                        String isFund = "有";
                        if (fund.intValue() == 0) {
                            isFund = "无";
                        }

                        String isSalaryMethod = "是";
                        if (salaryMethod.intValue() == 2) {
                            isSalaryMethod = "否";
                        }
                        Integer code = kwAydPushService.sendShanjie(name, mobile, city, isCar, age,
                                job, hasHouse, isPolicy, strSex, loanAmount, isSocialSecurity, isFund, isSalaryMethod);
                        kwAydFilter.setSuited("Y");
                        kwAydFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "shanjie", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }
                insert(kwAydFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

}
